// pages/favorite/favorite.js
Page({
  data: {
    currentTab: 0, // 默认显示“景点收藏”
    favoriteScenic: [],
    favoriteFood: [],
    favoriteActive: [],
  },

  onLoad: function () {
    const userKey = wx.getStorageSync('userKey');
    if (!userKey) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000,
      });
      return;
    }

    // 请求景点和美食的收藏数据
    this.loadFavoriteScenic(userKey);
    this.loadFavoriteFood(userKey);
    this.loadFavoriteActive(userKey);
  },

  onShow: function () {
      // 检查是否需要刷新收藏数据
      const refreshFavorites = wx.getStorageSync('refreshFavorites');
      if (refreshFavorites) {
        wx.removeStorageSync('refreshFavorites'); // 清除标记
        const userKey = wx.getStorageSync('userKey');
        this.loadFavoriteScenic(userKey);  // 重新加载景点收藏数据
        this.loadFavoriteFood(userKey);    // 重新加载美食收藏数据
        this.loadFavoriteActive(userKey);
      }
    },

  

  loadFavoriteScenic: function (userKey) {
    wx.request({
      url: `http://106.225.204.10:8888/travel/favorite/getScenic`,
      method: 'GET',
      data: { userKey: userKey },
      header: {
                      'openid':userKey
                    },
      success: (res) => {
        if (res.data.code === 200) {
          console.log('景点收藏数据:', res.data.data); // 打印景点数据
          this.setData({
            favoriteScenic: res.data.data || [],
          });
        } else {
          console.error('加载景点收藏数据失败:', res.data.message);
        }
      },
      fail: (error) => {
        console.error('请求景点收藏数据失败:', error);
      },
    });
  },

  loadFavoriteFood: function (userKey) {
    wx.request({
      url: `http://106.225.204.10:8888/travel/favorite/getFood`,
      method: 'GET',
      data: { userKey: userKey },
      header: {
                    'openid':userKey
                    },
      success: (res) => {
        if (res.data.code === 200) {
          console.log('美食收藏数据:', res.data.data); // 打印美食数据
          this.setData({
            favoriteFood: res.data.data || [],
          });
        } else {
          console.error('加载美食收藏数据失败:', res.data.message);
        }
      },
      fail: (error) => {
        console.error('请求美食收藏数据失败:', error);
      },
    });
  },
  
  loadFavoriteActive: function (userKey) {
    wx.request({
      url: `http://106.225.204.10:8888/travel/favorite/getActive`,    
      method: 'GET',
      data: { userKey: userKey },
      header: {
                      'openid':userKey
                    },
      success: (res) => {
        if (res.data.code === 200) {
          console.log('活动收藏数据:', res.data.data); // 打印景点数据
          this.setData({
            favoriteActive: res.data.data || [],
          });
          console.log('favoriteActive:', this.data.favoriteActive);
        } else {
          console.error('加载活动收藏数据失败:', res.data.message);
        }
      },
      fail: (error) => {
        console.error('请求景点收藏数据失败:', error);
      },
    });
    
  },



  // 切换标签页
  switchTab: function (event) {
    this.setData({
      currentTab: parseInt(event.currentTarget.dataset.index),
    });
  },

  // 跳转到详情页面
navigateToDetail: function(event) {
  const { id, type } = event.currentTarget.dataset;
  if (type === 'scenic') {
    wx.navigateTo({
      url: `/pages/scenicIntroduction/scenicIntroduction?id=${id}`
    });
  } 
   if (type === 'food') {
    wx.navigateTo({
      url: `/pages/foodDetail/foodDetail?id=${id}`
    });
  }
  else if (type === 'active') {
    wx.navigateTo({
      url: `/pages/active-detail/active-detail?id=${id}`
    });
  }
}
});
